﻿using System.IO;
using System.Text;
using System.Web;
using AjaxStack.Support;

namespace AjaxStack.Handlers
{
	/// <summary>
	/// Summary description for $codebehindclassname$
	/// </summary>
	public class CssHandler : IHttpHandler
	{
		public void ProcessRequest(HttpContext context)
		{
			context.Response.ContentType = "text/css";

			context.Response.Write(GetAppCss());
		}

		public static string GetAppCss()
		{
			var sb = new StringBuilder();

			var absolutePath = "~/css/".MapHostAbsolutePath();
			if (!Directory.Exists(absolutePath)) return null;

			foreach (var filePath in Directory.GetFiles(
				absolutePath, "*.css", SearchOption.AllDirectories))
			{
				if (filePath.InHiddenDirectory()) continue;

				//Can be optimized
				var cssText = File.ReadAllText(filePath);
				sb.AppendLine(cssText);
			}

			return ReleaseHandler.CompressCss(sb.ToString());
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}

}